package com.glory.brepda.activity.lanjian;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.LotAndPackagingfileEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.OnClick;

public class SFCBeforeTestInCZKActivity  extends BaseActivity {

    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.etMianQty)
    FreeUI_ClearEditText etMianQty;
    @BindView(R.id.xinghao)
    FreeUI_ClearEditText xinghao;
    @BindView(R.id.fengzuang)
    FreeUI_ClearEditText fengzuang;
    @BindView(R.id.custcode)
    FreeUI_ClearEditText custcode;
    @BindView(R.id.eqpment)
    FreeUI_ClearEditText eqpment;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etStepName)
    FreeUI_ClearEditText etStepName;

    private LotAndPackagingfileEntity entity;
    private String state="";   //状态需为等待作业

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_sfcbefore_test_in_czkactivity;
    }

    @Override
    protected void initView() {

        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);

        initEdit(etLotID, sa -> {
            //获取lot信息
            FreeUI_EntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>>
                    (new TypeToken<BaseResponseBean<LotAndPackagingfileEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        entity = response.body().getResponse().getBody();

                        xinghao.setText(entity.getLOT().getUDF().getProductModel());
                        fengzuang.setText(entity.getLOT().getMAINMATTYPE());
                        etMianQty.setText(entity.getLOT().getMAINQTY());
                        custcode.setText(entity.getLOT().getCUSTOMERCODE());
                        etStepName.setText(entity.getLOT().getSTEPDESC());
                        state = entity.getLOT().getSTATE();

                        showToast("成功", true);
                        etLotID.requestFocus();
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "失败");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("LOT_ID", getText(etLotID));
            map.addProperty("ACTION_TYPE", "QUERY");
            NetUtils.requestPostNet("IF.PDA_QUERY_LOT_PACKS", "Common", getApplication(), map, callBack);
        });
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit();
                break;
        }
    }

    private void submit(){

        if(getText(etLotID).equals("")){
            showToast("请先扫描批次信息", false);
            return;
        }

        if(getText(eqpment).equals("")){
            showToast("抽真空设备不能留空", false);
            return;
        }

        if(!getText(etStepName).equals("中间仓")){
            showToast("抽真空工步进站条件必须为中间仓工步!", false);
            return;
        }

        if(!state.equals("等待作业")){
            showToast("抽真空工步进站条件必须为中间仓工步等待作业状态!", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("成功", true);
                }
                etLotID.setText(null);
                xinghao.setText(null);
                fengzuang.setText(null);
                eqpment.setText(null);
                etMianQty.setText(null);
                custcode.setText(null);
                etStepName.setText(null);
                state = "";
                etLotID.requestFocus();
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "BEFORE_STEP_FOR_TEST");

            }
        };

        JsonObject map = new JsonObject();

        map.addProperty("LOT_ID", getText(etLotID));
        map.addProperty("PRODUCT_MODULE", getText(xinghao));
        map.addProperty("MAIN_MAT_TYPE", getText(fengzuang));
        map.addProperty("EQUIPMENT", getText(eqpment));
        map.addProperty("TRANS_IN_USER", Constant.CURRENT_USER);
        map.addProperty("ACTIONTYPE", "BRFORE_TEST_CZK");
        NetUtils.requestPostNet("IF.BEFORE_STEP_FOR_TEST", "Common", SFCBeforeTestInCZKActivity.this, map, callBack);
    }

    @Override
    protected void initData() {

    }

}